/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TareaINNERJOIN.java
 *
 * Created on 10-jun-2012, 20:45:26
 */
package MemoriaIntermedia.TAREA;

import MemoriaIntermedia.GestorMemoriaIntermedia;
import MemoriaIntermedia.Pagina;
import java.util.LinkedList;
import java.lang.*;

/**
 *
 * @author User
 */
public class TareaINNERJOIN extends javax.swing.JFrame {
    GestorMemoriaIntermedia gestor = new GestorMemoriaIntermedia(25);
    LinkedList<Prestatario>  TPrestatario = new LinkedList<Prestatario>();
    LinkedList<Cliente>  TCliente = new LinkedList<Cliente>();
    LinkedList<Object> Resultado = new LinkedList<Object>();


    /** Creates new form TareaINNERJOIN */
    public TareaINNERJOIN() {
        initComponents();
       jComboBox3.setVisible(false);
       jComboBox4.setVisible(false);
       jLabel3.setVisible(false);
       jLabel4.setVisible(false);
        TPrestatario.add(new Prestatario("Raul Rivas Miranda", 43356547));
        TPrestatario.add(new Prestatario("Rosa Silva Sosa", 71523345));
        TPrestatario.add(new Prestatario("Aydeé Sosa Arriola", 87676567));
        TPrestatario.add(new Prestatario("Victor Silva Huaman", 12089754));
        TPrestatario.add(new Prestatario("Wilfredo Marquez Boza", 78675400));
        TPrestatario.add(new Prestatario("Kelly Medrano Ñuflo", 43347687));
        
        TCliente.add(new Cliente("Maria Savedra Zuñiga", 43356547));
        TCliente.add(new Cliente("Christian Soto Bueno", 71523345));
        TCliente.add(new Cliente("Aydeé Sosa Arriola", 87676567));
        TCliente.add(new Cliente("Miriam Alegía Zevallos", 12089754));
        TCliente.add(new Cliente("Wilfredo Marquez Boza", 78675400));
        TCliente.add(new Cliente("Kelly Medrano Ñuflo", 43347687));
        // mostrar la tabla actual
        jComboBox1.removeAllItems();
        jComboBox2.removeAllItems();
        jComboBox3.removeAllItems();
        jComboBox4.removeAllItems();
        
        
        jComboBox1.addItem("TPrestatario");
        jComboBox3.addItem("TPrestatario");
        jComboBox3.addItem("TCliente");
        jComboBox1.addItem("TCliente");
        
        
        jComboBox2.addItem("Inner Join");
        jComboBox4.addItem("Nombre");
        jComboBox4.addItem("DNI");
        
        
        
        MostrarTabla();
        
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jComboBox3 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jComboBox4 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Seleccionar tabla:");

        jLabel2.setText("Seleccionar operacion:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                change2(evt);
            }
        });

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                changed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 613, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel3.setText("Inner join con la tabla:");

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("Sobre el atributo");

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButton1.setText("Resultado");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox2, 0, 515, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(32, 32, 32)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 633, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void changed(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_changed

    jComboBox3.setVisible(true);
    jComboBox4.setVisible(true);
    jLabel3.setVisible(true);
    jLabel4.setVisible(true);
    
    

    
}//GEN-LAST:event_changed

private void change2(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_change2

    
    if (jComboBox1.getSelectedItem() == "TPrestatario") {
        jComboBox3.removeAllItems();
        jComboBox3.addItem("TCliente");
        
    }
    else if (jComboBox1.getSelectedItem() == "TCliente")
    {
 jComboBox3.removeAllItems();
        jComboBox3.addItem("TPrestatario");
    }
   
    
    
    
    
}//GEN-LAST:event_change2

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

    InnerJoin(jComboBox4.getSelectedItem());
    
}//GEN-LAST:event_jButton1ActionPerformed
 
    private void InnerJoin( Object atributo)
    {
    // falta recuperar las tablas seleccionadas en caso  de que sean mas aparte de cliente y prestarario
     // todos los registros estan en  disco
        
        //colocar  casa  tupla de prestatario  al buffer del gestor
        int i=0;
    int j=0;
        for (Prestatario prestatario : TPrestatario) {
         
            Pagina pag = new Pagina(i,prestatario);
             gestor.ponerPaginaEnBuffer(pag);
            i++;
        }
        //colocar clientes 
          for (Cliente cliente : TCliente) {
         
              Pagina pag = new Pagina(j,cliente);
            
             // buscar la pagina actual en el buffer
              Pagina[] buffer = gestor.getBuffer();
              
              
              for (int k = 0; k < buffer.length; k++) {
                  if (buffer[k]!= null) {
                     Object p= buffer[k].getDatos();
                  if (p.equals(pag.getDatos()) ) {
                    //Escribirlo
                      jTextArea1.setText(jTextArea1.getText()+ pag.getDatos()+"\n");
                      
                  } 
                  }
                  else{
                      break;}
                  
                  
              }
              
              
              
             //
             gestor.ponerPaginaEnBuffer(pag);
             j++;
            
        }
          
          
      
        
        
        
        
        
        
        
    
    
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TareaINNERJOIN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TareaINNERJOIN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TareaINNERJOIN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TareaINNERJOIN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TareaINNERJOIN().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables

    private void MostrarTabla() {
        //mostrar siempre el resultado
        jTextArea1.setText("");
        for (Object o : Resultado) {
            jTextArea1.setText(jTextArea1.getText() + o.toString()+"\n");           
        }        
    }
    
    
}
